/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;
import View.*;
import Controller.*;
import Model.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 *
 * @author asus
 */
public class MainFrameGUI extends JFrame implements ActionListener,MouseListener{
    

    // The class attributes
    String sourceContact;
    Contact contact;
    URI uriMail;
    String[] tabValues={"Name ","Last Name ","Email ","Phone (perso)","Phone(pro) ","Home Add ","Comapny Add ","Home Page "};
    ContactList contactList;
    
    // Panels    
    private JPanel myPanel = new JPanel();
    private JPanel contactListPanel = new JPanel();
    private JPanel displayInfoPanel = new JPanel();
    private JPanel contactFormPanel=new JPanel();
    private JPanel buttonPanel=new JPanel();
    
    //Contact attributes
    private JLabel name = new JLabel("Name");
    private JLabel lastName = new JLabel("Last Name");
    private JLabel homeAdd = new JLabel("Home Address");
    private JLabel companyAdd = new JLabel("Company Address");
    private JLabel phonePerso = new JLabel("Phone number (perso)");
    private JLabel phonePro = new JLabel("Phone Number (pro)");
    private JLabel homePgae = new JLabel("Home Page");
    private JLabel email = new JLabel("Email");
    
    private JButton valid=new JButton("Save");
    private JButton clear=new JButton("Clear");
    
    // Menu parameters
    private JMenuBar menuBar = new JMenuBar(); 
    private JMenu option = new JMenu("Modify");
    private JMenu about = new JMenu("About");
    
    private JMenuItem createNew = new JMenuItem("Create New");
    private JMenuItem modify = new JMenuItem("Modify");

    // tab of JTextfields
    private JButton[] field = new JButton[200];
    private JTextField[] txtField = new JTextField[8];
     
    /*
     * Constructor
     */
    public MainFrameGUI(ContactList contactList){
        this.contactList=contactList;
        
        // Globlal parameters of the window
        this.setTitle("Address Book");
        this.setSize(800, 400);
        this.setLocationRelativeTo(null); 
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(myPanel);
        myPanel.setLayout(new BorderLayout());
        // The two parts are independant and manage in independant functions
        //generateDisplayInfoPanel();
        generateContactListPanel();
        generateContactFormPanel();
        generateMenu();
        generateButtonPanel();
        // They are a right and a left panel
        myPanel.add(contactListPanel, BorderLayout.WEST); // display the contact list
        myPanel.add(displayInfoPanel, BorderLayout.EAST); // disply information about a given contact
        myPanel.add(contactFormPanel, BorderLayout.CENTER);
        myPanel.add(buttonPanel, BorderLayout.SOUTH);
        this.setVisible(true);
    }
    
    /*
     * This method generate the Top menu bar with the options
     */
    private void generateMenu(){
        // Option Menu with 2 items
        option.add(createNew);
        option.add(modify);
        // Add the actionListener
        createNew.addActionListener(new CreateContactForm(contactList));
       // modify.addActionListener(new ChangeContact());
        // Add it to the menu Bar
        menuBar.add(option);
        menuBar.add(about);
        setJMenuBar(menuBar);
    }
  
   /*
    * Void fonction that generate the Contact list Panel (on the left side)
    */
    private void generateContactListPanel() {
        int i;
         ArrayList<String> List= new ArrayList<>();
         List= contactList.ConstructList();
         GridLayout layout = new GridLayout(6,2);
            contactListPanel.setBackground(Color.GRAY);
            contactListPanel.setLayout(layout);
         for (i=0;i<List.size();i++){
            field[i] = new JButton(List.get(i));            
            contactListPanel.add(field[i]);
            //field[i].setEditable(false);
            field[i].setBackground(Color.GRAY);
            field[i].setBorder(null);
            field[i].addActionListener(this);
            field[i].setActionCommand(contactList.getMyPhone(i));
            System.out.println(List.get(i));
         }
         
         
    }
     
    /*
     * Generate the central panel containing the form submition
     */
    private void generateContactFormPanel(){
        int i;
        contactFormPanel.setLayout(new BoxLayout(contactFormPanel, BoxLayout.Y_AXIS));
        contactFormPanel.setBackground(Color.LIGHT_GRAY);
             for (i=0;i<8;i++){
                txtField[i] = new JTextField(" ");
                contactFormPanel.add(txtField[i]);
                txtField[i].addActionListener(this);
             }
             
    }
    
    
    /*
     * Generate the panel with the buttons
     *
     */
    private void generateButtonPanel(){
        
        FlowLayout layout=new FlowLayout();
        buttonPanel.setLayout(layout);
        valid.addActionListener(new SaveFieldListener());
        clear.addActionListener(this);
        buttonPanel.add(clear);
        buttonPanel.add(valid);
    }
    
    /*
    * Void fonction that generate the Contact information panel (on the right side)
    */
    private void generateDisplayInfoPanel(String source) {
         // The information must be displyed as a list
        int i;
        GridLayout layout = new GridLayout(8,2);
        
        layout.setHgap(5);
        layout.setVgap(5);
        displayInfoPanel.setBackground(Color.LIGHT_GRAY);
        displayInfoPanel.setLayout(layout);
        contact =contactList.ReturnContact(sourceContact);
        
            JLabel lab1b= new JLabel(tabValues[0]);
            displayInfoPanel.add(lab1b);
            JLabel lab1 = new JLabel(contact.getNAME());
            displayInfoPanel.add(lab1);
            
            JLabel lab2b= new JLabel(tabValues[1]);
            displayInfoPanel.add(lab2b);
            JLabel lab2 = new JLabel(contact.getLAST_NAME());
            displayInfoPanel.add(lab2);
            
            JLabel lab3b= new JLabel(tabValues[2]);
            displayInfoPanel.add(lab3b);
            JLabel lab3 = new JLabel(contact.getEMAIL());
            displayInfoPanel.add(lab3);
            
           // uriMail=new URI(contact.getEMAIL(),message,null);            
            JLabel lab4b= new JLabel(tabValues[3]);
            displayInfoPanel.add(lab4b);
            JLabel lab4 = new JLabel(contact.getPHONE_PERSO());
            displayInfoPanel.add(lab4);
            
            JLabel lab5b= new JLabel(tabValues[4]);
            displayInfoPanel.add(lab5b);
            JLabel lab5 = new JLabel(contact.getPHONE_PRO());
            displayInfoPanel.add(lab5);
            
            JLabel lab6b= new JLabel(tabValues[5]);
            displayInfoPanel.add(lab6b);
            JLabel lab6 = new JLabel(contact.getHOME_ADD());
            displayInfoPanel.add(lab6);
            
            JLabel lab7b= new JLabel(tabValues[6]);
            displayInfoPanel.add(lab7b);
            JLabel lab7 = new JLabel(contact.getCOMPANY_ADD());
            displayInfoPanel.add(lab7);
            
            JLabel lab8b= new JLabel(tabValues[7]);
            displayInfoPanel.add(lab8b);
            JLabel lab8 = new JLabel(contact.getHOME_PAGE());
            displayInfoPanel.add(lab8);
            lab8.addMouseListener(this);

        System.out.println(contact.getNAME());
                     
    }
    
    /*
     * This methods permits to update a given panel
     */
    private void updatePanel(JPanel panel){
        panel.updateUI();
    }
    
    @Override
    public void actionPerformed (ActionEvent ev){ 
        displayInfoPanel.removeAll();
        displayInfoPanel.repaint();
        sourceContact = ev.getActionCommand();
        generateDisplayInfoPanel(sourceContact);
        displayInfoPanel.updateUI();
        System.out.println(ev.getActionCommand());
        
        
        
        
        
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        JLabel lab=(JLabel)e.getSource();
        String textToPass=null; 
        Desktop desk=Desktop.getDesktop();
        try {
            try {
                if (desk.isSupported(Desktop.Action.BROWSE)){
                    textToPass=lab.getText().replaceAll("\\<.*?\\>", "");
                    desk.browse(new URI(textToPass));
                }
               
            } catch (URISyntaxException ex) {
                Logger.getLogger(MainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(MainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
       
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }
    
    /*
     * This class has a method which create and save a new contact clicking on the save button
     */
    class SaveFieldListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            int i;
            String field=null;
            Contact contact = new Contact(
            txtField[0].getText(),
            txtField[1].getText(),
            txtField[2].getText(),
            txtField[3].getText(),
            txtField[4].getText(),
            txtField[5].getText(),
            txtField[6].getText(),
            txtField[7].getText());
            try {
                contactList.AddContact(contact);
                System.out.println(txtField[0].getText());
            } catch (IOException ex) {
                Logger.getLogger(MainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
             updatePanel(contactListPanel);
            
           
            //then clear :
            for (i=0;i<8;i++){
                txtField[i].setText(" ");
             }
            
             ArrayList<Contact> MyList = new ArrayList<>();
            try {
                MyList=contactList.ReturnList();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MainFrameGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
         
         for (i=0;i<MyList.size();i++){   
             System.out.println(MyList.get(i).getNAME());
        }
        }
        
        
    }
        
   
   
}
